package com.prj.ufdm.cloud.svr.gateway.bpo._impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.prj.ufdm.cloud.base.dto.RetData;
import com.prj.ufdm.cloud.base.util.BaseDateUtil;
import com.prj.ufdm.cloud.svr.gateway.bpo.SysFunctionBPO;
import com.prj.ufdm.cloud.svr.gateway.bpo.SysUserBPO;
import com.prj.ufdm.cloud.svr.gateway.config.RedisService;
import com.prj.ufdm.cloud.svr.gateway.constant.WebMsUrlConstants;
import com.prj.ufdm.cloud.svr.gateway.constant.WebSecurityConstants;
import com.prj.ufdm.cloud.svr.gateway.model.SysFunction;
import com.prj.ufdm.cloud.svr.gateway.model.SysUser;
import com.prj.ufdm.cloud.svr.gateway.util.RestTemplateUtil;

/**
 * 系统用户管理
 */
@Service 
public class SysUserBPOImpl  implements SysUserBPO {
	
	@Autowired
	RestTemplate restTemplate;
	@Autowired
	RedisService redisService;
	@Autowired
	SysFunctionBPO sysFunctionBPO;
	
	public SysUser modifyById(SysUser sysUser) throws Exception {
		RestTemplateUtil restTemplateUtil = new RestTemplateUtil(restTemplate);
		if(sysUser==null || sysUser.getId()==null || sysUser.getToken()==null || sysUser.getUsername()==null ) {
			return null;
		}
		Map<String,String> reqFormParamMap = new HashMap<String,String>();
		reqFormParamMap.put("id", sysUser.getId());
		reqFormParamMap.put("token", sysUser.getToken());
		reqFormParamMap.put("username", sysUser.getUsername());
		reqFormParamMap.put("tokenCreateTime", BaseDateUtil.getCurrentTime());
		RetData<SysUser> retSysUser = restTemplateUtil.reqPostForm(WebMsUrlConstants.MODIFY_SYSUSER_URL, null, reqFormParamMap, new TypeReference<RetData<SysUser>>(){});
    	return retSysUser.getRetBody();
	}

	@Override
	public SysUser doGetSysUserByUsername(String username) throws Exception {
		RestTemplateUtil restTemplateUtil = new RestTemplateUtil(restTemplate);
		if(username==null || username.equals("")) {
			return null;
		}
		Map<String,String> reqFormParamMap = new HashMap<String,String>();
		reqFormParamMap.put("username",username);
		RetData<SysUser> retSysUser = restTemplateUtil.reqPostForm(WebMsUrlConstants.GET_SYSUSER_BY_USERNAME_URL, null, reqFormParamMap, new TypeReference<RetData<SysUser>>(){});
		return retSysUser.getRetBody();
	}

	@Override
	public SysUser doGetSysUserByToken(String token) throws Exception {
		RestTemplateUtil restTemplateUtil = new RestTemplateUtil(restTemplate);
		if(token==null || token.equals("")) {
			return null;
		}
		Map<String,String> reqFormParamMap = new HashMap<String,String>();
		reqFormParamMap.put("token",token);
		RetData<SysUser> retSysUser = restTemplateUtil.reqPostForm(WebMsUrlConstants.GET_SYSUSER_BY_TOKEN_URL, null, reqFormParamMap, new TypeReference<RetData<SysUser>>(){});
		return retSysUser.getRetBody();
	}

	@Override
	public void modifyByRedis(SysUser sysUser) throws Exception {
		
		// 如果不允许一个账号同时登录，删除所有缓存信息
		if(!"1".equals(sysUser.getEnableParallelLogin())) {
			Set<String> keys = redisService.getKeys(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO+":*");
			for(String key:keys) {
				Object redisObject = redisService.get(key);
				SysUser redisSysUser = JSON.parseObject(redisObject!=null?redisObject.toString():null, new TypeReference<SysUser>(){}.getType());
				if(redisSysUser!=null && redisSysUser.getId()!=null  &&  redisSysUser.getId().equals(sysUser.getId())) {
					redisService.del(key);
				}
			}
		}
		
		// 设置 “TOKEN：用户信息”
		redisService.set(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO+":"+sysUser.getToken(), sysUser,WebSecurityConstants.TOKEN_VALID_MINUTES*60);
		// 设置 “USERID：用户信息”
		redisService.set(WebSecurityConstants.REDIS_MAP_KEY_USERID_USERINFO+":"+sysUser.getId(), sysUser);
		// 设置 “USERID：授权功能”
		List<SysFunction> listUserSysFunction = sysFunctionBPO.doGetFunctionListByUserId(sysUser.getId());
		redisService.set(WebSecurityConstants.REDIS_MAP_KEY_USERID_FUNCTION+":"+sysUser.getId(), listUserSysFunction);	
	}

}
